###########################################################################################

library(data.table)
library(ggplot2)
library(optparse)
library(stringr)

###########################################################################################

option_list <- list(
  make_option(c("--purity_file"), type = "character", help = ""),
  make_option(c("--disease"), type = "character"),
  make_option(c("--prefix"), type = "character"),
  make_option(c("-o","--out_path"), type = "character")
)

if(1!=1){
  purity_file <- "/public/user/xxf/20210520_LCSomatic/titan/Purity_titan.final.tsv"
  disease <- "LUAD"
  out_path <- "/public/user/xxf/20210520_LCSomatic/titan"
  prefix <- "Final"
}

###########################################################################################

parseobj <- OptionParser(option_list=option_list, usage = "usage: Rscript %prog [options]")
opt <- parse_args(parseobj)
print(opt)

purity_file <- opt$purity_file
disease <- opt$disease
out_path <- opt$out_path
prefix <- opt$prefix

###########################################################################################

dat <- data.frame(fread(purity_file))
dat$Class <- disease
images_path <- out_path

###########################################################################################

dat1 <- dat
dat$Purity[is.na(dat$Purity)] <- 0
dat$Ploidy[is.na(dat$Ploidy)] <- 2

###########################################################################################

images_name <- paste(images_path,"/Purity" , prefix ,".pdf",sep="")

plot <- ggplot(data=dat,mapping = aes(x=Class,y=Purity))+
  geom_boxplot(lwd=1.5,aes(color=Class)) +
  geom_jitter(position=position_jitter(0.2),aes(color=Class)) +
  xlab(NULL) +
  ylab('Purity')+
  theme_bw() +
  ylim(-0.1,1) +
  theme(panel.background = element_blank(),#设置背影为白色#清除网格线
        legend.position ='right',
        legend.title = element_blank() ,
        panel.grid.major=element_line(colour=NA),
        legend.text = element_text(size = 8,color="black",face='bold'),
        axis.text.x = element_text(size = 15,color="black",face='bold'),
        axis.text.y = element_text(size = 15,color="black",face='bold'),
        axis.title.x = element_text(size = 18,color="black",face='bold'),
        axis.title.y = element_text(size = 18,color="black",face='bold'),
        axis.line = element_line(size = 0.5)) 
ggsave(file=images_name,plot=plot,width=5,height=6)



images_name <- paste(images_path,"/Ploidy" , prefix , ".pdf",sep="")

plot <- ggplot(data=dat,mapping = aes(x=Class,y=Ploidy))+
  geom_boxplot(lwd=1.5,aes(color=Class)) +
  geom_jitter(position=position_jitter(0.2),aes(color=Class)) +
  xlab(NULL) +
  ylab('Ploidy')+
  theme_bw() +
  ylim(1,5) +
  theme(panel.background = element_blank(),#设置背影为白色#清除网格线
        legend.position ='right',
        legend.title = element_blank() ,
        panel.grid.major=element_line(colour=NA),
        legend.text = element_text(size = 8,color="black",face='bold'),
        axis.text.x = element_text(size = 15,color="black",face='bold'),
        axis.text.y = element_text(size = 15,color="black",face='bold'),
        axis.title.x = element_text(size = 18,color="black",face='bold'),
        axis.title.y = element_text(size = 18,color="black",face='bold'),
        axis.line = element_line(size = 0.5)) 
ggsave(file=images_name,plot=plot,width=5,height=6)

###########################################################################################
quantile(dat1$Purity , na.rm=T , probs = seq(0,1,0.1))